﻿///------------------------
///引用计数器工具
///------------------------

using UnityEngine;

namespace FrameWorkSong
{
    public interface IRefCounter
    {
        int RefCount { get; }
        void Retain(Object refOwner = null);
        void Release(Object refOwner = null);

    }
    /// <summary>
    /// 引用计数器
    /// </summary>
    public class SimpleRefCount : IRefCounter 
    {
        public int RefCount
        {
            get;
            private set;
           
        }
        /// <summary>
        /// 释放
        /// </summary>
        /// <param name="refOwner"></param>
        public void Release(Object refOwner = null)
        {
            RefCount--;
            if (RefCount == 0)
            {
                OnZeroRef();
            }
        }
        /// <summary>
        /// 持有
        /// </summary>
        /// <param name="refOwner"></param>
        public void Retain(Object refOwner = null)
        {
            RefCount++;
        }
        /// <summary>
        /// 释放虚方法
        /// </summary>
        protected virtual void OnZeroRef()
        {
            
        }


      
    }

}
